<?php

require_once APPLICATION_PATH . '/../library/ckeditor/ckeditor.php';

    class Admin_BlogController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->list_sess_unset('controller_blog');
            $this->session_search_key = 'controller_blog';
            $this->session_search_name = 'search_label';
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'blog','admin');
        }

        public function viewAction() {
            if ($this->_getParam('blog_id') && $this->_getParam('blog_id') != '') {
                $blog_id = $this->_getParam('blog_id');
            } else {
                $this->_helper->redirector('list', 'blog', 'admin');
            }
            $blogModel = new Model_Blog();
            $blogalldata = $blogModel->fetchEntryById($blog_id);
            $this->view->blogalldata = $blogalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'blog_id';
            $sort = 'desc';

           
            /* Pagination Login */
            $itemsPerPageReviews = $this->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                 
                /*search code here*/
                if ($postedData['searchtype'] == '') {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                if (isset($postedData['searchtype']) && ($postedData['searchtype'] != '')) {
                    $searchtype = $postedData['searchtype'];
                    $where = " blog_title LIKE '%$searchtype%' OR blog_description LIKE '%$searchtype%'  OR blog_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
			      
                }    
                
            } else {

                if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) 
                    {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                    $searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
                    $where = " blog_title LIKE '%$searchtype%'   OR blog_description LIKE '%$searchtype%'  OR blog_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
            }

            $blogModel = new Model_Blog();
            
            $blogsData = $blogModel->fetchBlog($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal($blogModel->getBlogCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('module'=>'admin','controller' => 'blog', 'action' => 'list', 'page' => 'PAGENO'), null, false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
           
            $this->view->blogsData = $blogsData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $blogModel->getStatusArray();
            $this->view->searchtype = $searchtype;
            
        }public function deleteAction($ids = Array()) {
            //Load blog Model 
            $blogModel = new Model_Blog();
            if ($this->_getParam('blog_id') && $this->_getParam('blog_id') != '') {
                $blog_id = $this->_getParam('blog_id');
                $blogModel->deleteBlog($blog_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'blog', 'admin');
            } else {
                $blogModel->deleteBlog($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'blog', 'admin');
            }
        }
     public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $blogData = Array();
            $this->view->mode = 'Add';

            //Load blog Model
            $blogModel = new Model_Blog();

            //Load blog Form
            $blogForm = new Form_Admin_BlogForm();

            //Load the status array for the select box in form 
            $statusArray = $blogModel->getStatusArray();
            $blogForm->blog_status->addMultiOptions($statusArray);

            //Check for the blog Parameter
            $blogData['blog_id'] = $this->_request->getParam('blog_id');
            $blog_id = (!empty($blogData['blog_id'])) ? $blogData['blog_id'] : null;
            if (!empty($blog_id)) {
            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $blogModel->fetchEntryById($blog_id);
            }
            //Populate form 
            $this->view->form = $blogForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if (!$blogForm->isValid($postedData)) {
                    $errorMessage = $blogForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        if ($this->view->mode == 'Add') {
                            $blogData['blog_createddate'] = date('Y-m-d H:i:s');
                        }
                        //blog save
                        $blogData['blog_id'] = $blog_id;
                        $blogData['blog_title'] = $postedData['blog_title'];
                        
                        $blogData['blog_shortdescription'] = $postedData['blog_shortdescription'];
                        $blogData['blog_description'] = $postedData['blog_description'];
                       // $blogData['blog_createddate'] = $postedData['blog_createddate'];
                        $blogData['blog_updateddate'] = date('Y-m-d H:i:s');
                        $blogData['blog_status'] = $postedData['blog_status'];
						

                        $blogModel->save($blogData, 'blog_id');
                        if ($this->view->mode == 'Add') {
                            $successmessage = 'Blog add successfully';
                        } else {
                            $successmessage = 'Blog updated successfully';
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'blog', 'admin');
                    }
                }
            }
            $this->view->blogid = $blog_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $blogForm;


    }
    }?>